<?php //callback for ajax/json
/**
 * @review is this used? might be old, before the api was made
 * these should be all in the model anyway, if you're calling 
 * this file, look into the api.  this has been marked as
 * depreciated 08/23/08 -dn
 * @deprecated 
 */
class detail_query {

function __construct(){
	$this->site_to_query = 'http://www.booksite.com/texis/scripts/oop/rawsearch_ing/rawdetail.html?sid=3401';
	$this->url = $this->site_to_query.'&amp;isbn='.$_GET['isbn'];
}

function _runCurl($site_to_query, $return = FALSE)
	{
	 	$this->ch = curl_init();
		curl_setopt($this->ch, CURLOPT_URL, $site_to_query);
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); 
		if($return){
			$results = curl_exec($this->ch);
			curl_close($this->ch);
			return $results;
		}else{
			$this->_results = curl_exec($this->ch);
			curl_close($this->ch);
		}
		//var_dump('closed');
	}//end runcurl
	
function _parseDetails($results)
	{
		//var_dump($this->_results);
		$document_array = explode('|',$results);
		//var_dump($document_array);
		//!!!000|warehouse|Title|Author|Publisher_Name|PUB_DATE|ISBN|Binding|Price|Disc_Key|GRAPHICS|Annotation|
		$booksite_legend_2 = array(
							'0'	=>	'warehouse',
							'1'	=>	'title',
							'2'	=>	'author',
							'3'	=>	'publisher_name',
							'4'	=>	'pub_date',
							'5'	=>	'isbn',
							'6'	=>	'binding',
							'7'	=>	'price',
							'8'	=>	'disc_key',
							'9'	=>	'graphics',
							'10'=>	'annotation'
							);
	
		$object = new stdclass();
		//$object->result_set = new stdclass();
		$header = array_shift($document_array);
		//$object->rows = array_shift($document_array);
		$chunked = array_chunk($document_array, 11);
		//var_dump($chunked);
		foreach($chunked[0] as $key=>$value){ //each row as array
			//var_dump($value);
			$object->$booksite_legend_2[$key] = $value;
			//foreach($value as $column=>$data){ //each column of data in row
			//	$object->result_set->$key->$booksite_legend_1[$column] = $data;
			//}//end of foreach data bit
	 	}//end foreach
		return $object;
	 }//end _parseDetails
	 
	/**
	*	provide an isbn to query web service 
	*/	 
	 function performQuery($product){
	 		//return $product;
			if(empty($product)){
				$product = $_GET['isbn'];
			}
	 	if(!empty($product)){
			$url = $this->site_to_query.'&amp;isbn='.$product;
			$this->_runCurl($url);
			$obj = $this->_parseDetails($this->_results);
			$generic_img = 'images/no_book_image.gif';
			if(empty($obj->graphics) || strstr($obj->graphics, 'http:') == FALSE){
				$obj->graphics = $generic_img;
			}//end if graphic path
		}//end if product
	//$ann = preg_quote($obj->annotation);
		$ann = $obj->annotation;
		//$ann =  "<p>this is a \nreturn</p>";
		$ann = str_replace("\x0D\x0A","",$ann); //remove windows carriage return/newline
		//$ann = str_replace("\x0D","",$ann);
		//$ann = preg_replace('/\x0A/','',$ann);
		$ann = preg_replace('/"/','\"',html_entity_decode($ann));
		$ann = preg_replace("/'/","\'",$ann);
		//$ann = htmlentities($ann, ENT_QUOTES);
		 if(empty($ann)){
		 	$ann = 'No abstract available';
		 }
		$str = '{"annotation":"'
		.$ann
		.'","graphics":"'
		.$obj->graphics.'"}';
		
		return $str;
	 }//end performQuery
}//end class

/**
*	TODO: make an AJAX api to move this logic out of the class file
*/
//if calling from with joomla, do nothing
//if calling directly create object and echo a JSON string

if(!defined( '_JEXEC' )){
	$query = new detail_query();
	$JSONstr = $query->performQuery($_GET['isbn']);
	echo $JSONstr;
}

?>